package com.adcc.swingui.base;

/*
 * @(#)VsnetOptionsDialog.java
 *
 * Copyright 2002 - 2003 JIDE Software Inc. All rights reserved.
 */

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import com.jidesoft.dialog.ButtonNames;
import com.jidesoft.dialog.ButtonPanel;
import com.jidesoft.dialog.MultiplePageDialog;
import com.jidesoft.swing.PartialEtchedBorder;

/**
 * 系统设置对话框类 Demoed Component: {@link MultiplePageDialog} <br>
 * Required jar files: jide-common.jar, jide-dialogs.jar <br>
 * Required L&F: Jide L&F extension required
 */
public class SetOptionsDialog extends MultiplePageDialog {
	private static final long serialVersionUID = 1L;

	private String okActionName = "确定";
	private String cancelActionName = "取消";
	private String applyActionName = "应用";

	public SetOptionsDialog(Frame owner, String title) throws HeadlessException {
		super(owner, title);
	}

	/**
	 * 带有初始化界面按钮名称参数的构造方法
	 * 
	 * @param actionBtnNames界面操作按钮
	 *            （确定-取消-应用）
	 * @throws HeadlessException
	 */
	public SetOptionsDialog(Frame owner, String title, String... actionBtnNames) throws HeadlessException {
		super(owner, title);
		for (int i = 0; i < actionBtnNames.length; i++) {
			switch (i) {
			case 0:
				okActionName = actionBtnNames[i];
				break;
			case 1:
				cancelActionName = actionBtnNames[i];
				break;
			case 2:
				applyActionName = actionBtnNames[i];
				break;
			default:
				break;
			}
		}
	}

	@Override
	protected void initComponents() {
		super.initComponents();
		getContentPanel().setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10));
		getIndexPanel().setBackground(Color.white);
		getIndexPanel().setPreferredSize(new Dimension(160, 400));
		getButtonPanel().setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		getPagesPanel().setBorder(
				BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0),
						createSeparatorBorderAtBottom()));
	}

	@Override
	public ButtonPanel createButtonPanel() {
		ButtonPanel buttonPanel = super.createButtonPanel();
		AbstractAction okAction = new AbstractAction(okActionName) {
			private static final long serialVersionUID = -8212152795901394544L;

			public void actionPerformed(ActionEvent e) {
				setDialogResult(RESULT_AFFIRMED);
				for (int i = 0; i < SetOptionsDialog.this.getPageList().getSize(); i++) {
					DefaultOptionsPanel panel = ((DefaultOptionsPanel) SetOptionsDialog.this.getPageList().getPage(i));
					if (panel.isInited()) {
						panel.apply();// 保存
					}
				}

				setVisible(false);
				dispose();
			}
		};
		AbstractAction cancelAction = new AbstractAction(cancelActionName) {
			private static final long serialVersionUID = -9124324443138164926L;

			public void actionPerformed(ActionEvent e) {
				setDialogResult(RESULT_CANCELLED);
				for (int i = 0; i < SetOptionsDialog.this.getPageList().getSize(); i++) {
					DefaultOptionsPanel panel = ((DefaultOptionsPanel) SetOptionsDialog.this.getPageList().getPage(i));
					if (panel.isInited()) {
						panel.registerModel();// 还原成model中的
					}
				}
				setVisible(false);
				dispose();
			}
		};

		AbstractAction applyAction = new AbstractAction(applyActionName) {
			private static final long serialVersionUID = -9124324443138164926L;

			public void actionPerformed(ActionEvent e) {
				setDialogResult(RESULT_AFFIRMED);
				((DefaultOptionsPanel) SetOptionsDialog.this.getPageList().getCurrentPage()).apply();// 保存当前
			}
		};
		((JButton) buttonPanel.getButtonByName(ButtonNames.OK)).setAction(okAction);
		((JButton) buttonPanel.getButtonByName(ButtonNames.CANCEL)).setAction(cancelAction);
		((JButton) buttonPanel.getButtonByName(ButtonNames.APPLY)).setAction(applyAction);
		setDefaultCancelAction(cancelAction);
		setDefaultAction(okAction);
		return buttonPanel;
	}

	@Override
	public Dimension getPreferredSize() {
		return new Dimension(800, 600);
	}

	private static Border createSeparatorBorderAtBottom() {
		return new PartialEtchedBorder(EtchedBorder.LOWERED, PartialEtchedBorder.SOUTH);
	}

}
